package com.zhehekeji.opsys.api.webservice.server;

import javax.jws.WebService;

import org.apache.cxf.interceptor.InInterceptors;
import org.apache.cxf.interceptor.OutInterceptors;
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhehekeji.opsys.api.code.LuckyVoteResultCode;
import com.zhehekeji.opsys.api.resp.GetThemeListResp;
import com.zhehekeji.opsys.api.resp.GetThemeMovieListResp;
import com.zhehekeji.opsys.api.resp.GetThemeMovieResp;
import com.zhehekeji.opsys.api.resp.GetThemeResp;
import com.zhehekeji.opsys.api.resp.GetThemeUserListResp;
import com.zhehekeji.opsys.api.resp.LuckySignupResp;
import com.zhehekeji.opsys.api.service.manager.ThemeManagerService;

/**
 * @ClassName ThemeManagerWS
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-5-25   上午10:39:49
 */
@InInterceptors(classes={GZIPInInterceptor.class})
@OutInterceptors(classes={GZIPOutInterceptor.class})
@WebService(endpointInterface="com.zhehekeji.opsys.api.webservice.server.ThemeManager",
name="themeManager",portName="themeManagerPort",serviceName="themeManagerWS")
public class ThemeManagerWS implements ThemeManager {
	private static final Logger log = LoggerFactory.getLogger(ThemeManagerWS.class);
	@Autowired
	private ThemeManagerService themeManagerService;
	
	@Override
	public GetThemeResp getTheme(String themeCode) {
		
		GetThemeResp resp = themeManagerService.getTheme(themeCode);
		return resp;
		
	}

	@Override
	public GetThemeListResp getThemeList(long cpCode, int firstIndex, int maxResult) {
		
		GetThemeListResp resp = themeManagerService.getThemeList(cpCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public GetThemeMovieResp getThemeMovie(String movieCode) {
		
		GetThemeMovieResp resp = themeManagerService.getThemeMovie(movieCode);
		return resp;
		
	}

	@Override
	public GetThemeMovieListResp getThemeMovieList(String themeCode, int firstIndex, int maxResult) {
		
		GetThemeMovieListResp resp = themeManagerService.getThemeMovieList(themeCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public LuckySignupResp luckySignup(String movieCode, String productID, String userID, String userPhone, int updatePhone) {
		LuckySignupResp resp = new LuckySignupResp();
		try{
			resp = themeManagerService.luckySignup(movieCode, productID, userID, userPhone, updatePhone);
		}catch(Exception e1){
			//由javax.persistence.OptimisticLockException这里共享锁抛出的异常，再执行一次
			try{
				log.info(String.format("用户[%s]参与观影[%s]报名时异常，事务回滚，程序自动控制，再报一次！", userID, movieCode));
				resp = themeManagerService.luckySignup(movieCode, productID, userID, userPhone, updatePhone);
			}catch(Exception e2){
				String errorDesc = String.format("用户[%s]参与观影[%s]报名时异常，事务回滚，报名失败！", userID, movieCode);
				resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
				resp.setResultDesc(errorDesc);
				log.error(errorDesc);
			}
		}
		
		return resp;
		
	}
	
	@Override
	public GetThemeUserListResp getThemeUserList(String themeCode, int type, int firstIndex, int maxResult) {
		
		GetThemeUserListResp resp = themeManagerService.getThemeUserList(themeCode, type, firstIndex, maxResult);
		return resp;
		
	}

}

